<?php
class Dataload extends CI_Model {

	var $orgid;
	
	function __construct()
	{
		parent::__construct();

		$this->orgid=$this->tank_auth->get_user_id();

	}

	private function handleRequest($method,$paramArray){

		$request = xmlrpc_encode_request($method, $paramArray);
		$context = stream_context_create(array('http' => array(
			    'method' => "POST",
			    'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
			    'content' => $request
		)));

		$server = 'http://localhost/sunshopwebsvc/xmlrpc.php';

		$file = file_get_contents($server, false, $context);

		$response = xmlrpc_decode($file);

		return $response;
	}

	/* Managed Objects */
	public function fetchManagedObjects(){

	}

	public function addManagedObjects(){

	}

	public function deleteManagedObjects(){

	}




	/*Categories */
	public function fetchCategories(){

		$request=$this->handleRequest("get_categories", $this->orgid);
		return $request;
	}

	public function addCategories(){

	}

	public function delCategories(){

	}

	public function updateCategories(){

	}


	/* Products */

	public function fetchProducts(){
		$request=$this->handleRequest("get_all_objects", array('orgid'=>$this->orgid));
		
		return $request;
	}
	
	public function addProducts(){

	}

	public function delProducts(){

	}

	public function updateProducts(){

	}



	/* Product attributes */

	public function fetchProductAttributes(){
	
	}
	
	public function addProductAttributes(){

	}

	public function delProductAttributes(){

	}

	public function updateProductAttributes(){

	}
	
	
	
	
	/* Workflow */
	
	public function index()
	{
	
		$this->db->select('*');
		$this->db->from('workflow');
		$this->db->join('workflowservicemapping', 'workflowservicemapping.workflow_id = workflow.WorkflowID');
		$this->db->join('service', 'workflowservicemapping.service_id = service.ServiceID');
		$query=$this->db->get();
	
		$workFlowArray=array();
	
		foreach ($query->result() as $row)
		{
			print_r($row);
			echo "<br/><hr/><br/>";
		}
	
	
	}
	
	public function fetchAllWorkFlows(){
	
		$this->db->select('WorkflowID,name');
		$this->db->from('workflow');
		$this->db->where('OrgID',0);
		$this->db->or_where('OrgID',$this->orgid);
	
		$query=$this->db->get();
		$workFlowArray=array();
	
		foreach($query->result() as $row){
			$workFlow=array(
					'workflow_id' => $row->WorkflowID,
					'workflow_name' => $row->name
				
			);
			$workFlowArray[]=$workFlow;
		}
	
		return $workFlowArray;
	
	}
	
	public function fetchWorkflowData(){
	
		$wfid=$this->input->post("wfid",TRUE);
	
		$this->db->select('*');
		$this->db->from('workflow');
		$this->db->join('workflowservicemapping', 'workflowservicemapping.workflow_id = workflow.WorkflowID');
		$this->db->join('service', 'workflowservicemapping.service_id = service.ServiceID');
		$this->db->where('workflowservicemapping.workflow_id', $wfid);
		$this->db->order_by('screen_position asc, screen_order asc');
		$query=$this->db->get();
	
	
	
		if($query->num_rows()  > 0){
			$workFlowArray=array();
				
			//Get first row and populate workflow data
			$row = $query->row();
			$workFlowArray['workflow_service_id'] = $row->workflow_id;
			$workFlowArray['workflow_name'] = $row->name;
				
			$servicesArray=array();
			foreach ($query->result() as $row)
			{
				$service=array();
				$service['ws_id']=$row->ws_id;
				$service['service_id']=$row->service_id;
				$service['descr']=$row->descr;
				$service['screen_position']=$row->screen_position;
				$service['screen_order']=$row->screen_order;
					
				$servicesArray[] = $service;
			}
				
			$workFlowArray['service_array']=$servicesArray;
				
				
			return $workFlowArray;
				
		}else{
			return array('error'=>'1');
		}
	
	}
}